
package com.scalar.chiptrack.tags;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;

import com.scalar.chiptrack.commons.SessionKeys;

/**
 * Custom tag that retrieves and renders 
 * all messages from request object
 */
public class InfoTag extends org.apache.struts.taglib.html.ErrorsTag {

    private final static String FONT_COLOR = "green";
    
    /**
     * class property
     */
    private String m_styleClass = "";
    
    public String getStyleClass() {
        return m_styleClass;
    }
    public void setStyleClass (String styleClass) {
        if (styleClass != null) {
            m_styleClass = styleClass;
        }
    }

    
    public int doStartTag() throws JspException {

        HashMap messages = (HashMap) pageContext.getRequest().getAttribute (SessionKeys.INFO_MESSAGES);

        if (messages == null) {
	    return (EVAL_BODY_INCLUDE);
        }

        // Render the messages
        Object[] keys = messages.keySet().toArray();
        Object[] values = messages.values().toArray();
        
        StringBuffer report = new StringBuffer();
        report.append ("<table border='0' bordercolor='#FFFFFF'>");

        for (int i=0; i<keys.length; i++) {
            report.append ("<tr class='" + m_styleClass + "'><td align='left'><font color='" + FONT_COLOR + "'><bold><li>" + (String) keys[i]);

            if (values[i] == null) {
                report.append ("</bold></font></td>");
            }
            else {
                report.append (": </bold></font></td><td align='left'><font color='" + FONT_COLOR + "'>" + (String) values[i] + "</font></td></tr>");
            }
        }
        report.append ("</table>");
          
        try {
            //Print the results to our output writer
            pageContext.getOut().print(report.toString());
        }
        catch (IOException ioe) {
            System.out.println ("Exception while rendering error messages");
            System.out.println ("Exception Message: " + ioe.getMessage());
        }
        
        return (EVAL_BODY_INCLUDE);
    }
    
}